/**
 * 
 */
package com.buss.base.dao.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.buss.base.dao.CompanyDao;
import com.buss.base.model.Company;
import com.buss.core.dic.RecordStatus;
import com.buss.core.persistence.BaseDao;
import com.buss.core.persistence.Paginalbe;
import com.buss.core.persistence.ResultList;
import com.buss.core.utils.StringUtil;

/**
 * @author 高小勇
 *
 */
@Repository
public class CompanyDaoImpl implements CompanyDao {

	@Autowired
	private BaseDao<Company> baseDao;
	
	@Override
	public void saveCompany(Company company) {
		baseDao.save(company);
	}


	@Override
	public void editCompany(Company company) {
		baseDao.edit(company);
	}


	@Override
	public void updateStatus(String[] ids, RecordStatus status) {
		String sql = "update Company company set company.status = :status where company.id in (:ids)";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("status", status);
		map.put("ids", ids);
		baseDao.excuteHsql(sql, map);
	}

	@Override
	public Company loadCompany(String id) {		
		return baseDao.load(id, Company.class);
	}

	@Override
	public ResultList<Company> queryCompany(String name, String pinyin,
			String address,Paginalbe page) {
		String sql = "select company from Company company where 1 = 1 ";
		Map<String, Object> map = new HashMap<String, Object>();
		if(StringUtil.isNotEmpty(name)){
			sql += " and company.name like :name ";
			map.put("name", "%" + name.trim() + "%");
		}
		if(StringUtil.isNotEmpty(pinyin)){
			sql += " and company.pinyin like :pinyin ";
			map.put("pinyin", "%" + pinyin.trim() + "%");
		}
		if(StringUtil.isNotEmpty(address)){
			sql += " and company.address like :address ";
			map.put("address", "%" + address.trim() + "%");
		}
		return baseDao.queryHsql(sql, map, page);
	}


	@Override
	public void removeCompany(String[] ids) {
		String sql = "delete from Company company where company.id in (:ids)";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ids", ids);
		baseDao.excuteHsql(sql, map);
	}

}
